<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>Webpack基础配置(三) | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="前言 问：为什么使用loader？    webpack自身只能理解js类型的文件，loader可以将所有类型的文件转换为webpack可以处理的有效模块。    问：本节有哪些loader？  style-loader、css-loader、less-loader、sass-loader、postcss-loader    .css 文件css-loader   下载相关loader yarn">
<meta property="og:type" content="article">
<meta property="og:title" content="Webpack基础配置(三)">
<meta property="og:url" content="http://example.com/2020/12/20/Webpack%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE-%E4%B8%89/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="前言 问：为什么使用loader？    webpack自身只能理解js类型的文件，loader可以将所有类型的文件转换为webpack可以处理的有效模块。    问：本节有哪些loader？  style-loader、css-loader、less-loader、sass-loader、postcss-loader    .css 文件css-loader   下载相关loader yarn">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/5bca2c4b390f4823b6a206738c131ca1~tplv-k3u1fbpfcp-watermark.image">
<meta property="og:image" content="https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/7410501f54bd46fb8a2609041fbaa3c8~tplv-k3u1fbpfcp-watermark.image">
<meta property="og:image" content="https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/c84453253e4e45eea029fa63c79c491b~tplv-k3u1fbpfcp-watermark.image">
<meta property="og:image" content="https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/f8df99b0cdeb4e8f9f963fbcd9007e0c~tplv-k3u1fbpfcp-watermark.image">
<meta property="og:image" content="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/f540e4471725481e9136521c745db58b~tplv-k3u1fbpfcp-watermark.image">
<meta property="og:image" content="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/76f1b62d490041efadbe81bff047af5e~tplv-k3u1fbpfcp-watermark.image">
<meta property="og:image" content="https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/67f217a66880484cb0c4ef6a93746c50~tplv-k3u1fbpfcp-watermark.image">
<meta property="og:image" content="https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/224dd269b7994ccfa5ae976e55306d16~tplv-k3u1fbpfcp-watermark.image">
<meta property="og:image" content="https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/f8f19544a4ad4231a8c430cb5ccea6a0~tplv-k3u1fbpfcp-watermark.image">
<meta property="og:image" content="https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/9139fb82e8894cdfa5e790545ba0b74f~tplv-k3u1fbpfcp-watermark.image">
<meta property="og:image" content="https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/1e849eaa11ba49499d42961f3d51e568~tplv-k3u1fbpfcp-watermark.image">
<meta property="og:image" content="https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/33f41c78c752485ebb2d4844d26c3634~tplv-k3u1fbpfcp-watermark.image">
<meta property="og:image" content="https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/6f74173328f746759f61f7160e9bc898~tplv-k3u1fbpfcp-watermark.image">
<meta property="og:image" content="https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/991024b545284d719040e0f55bc38278~tplv-k3u1fbpfcp-watermark.image">
<meta property="og:image" content="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/f6cd7df5e9ad49b98f0974ecb0353972~tplv-k3u1fbpfcp-watermark.image">
<meta property="og:image" content="https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/bb49c688635445a6be54c2fe51059567~tplv-k3u1fbpfcp-watermark.image">
<meta property="og:image" content="https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/04fad7fb512248f59c46dc71e7507436~tplv-k3u1fbpfcp-watermark.image">
<meta property="og:image" content="https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/7a54d55a11594ad0910931e93b5dcf39~tplv-k3u1fbpfcp-watermark.image">
<meta property="og:image" content="https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/0e640732458645a790453b9bc658f28d~tplv-k3u1fbpfcp-watermark.image">
<meta property="og:image" content="https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/1e07ad8e71074ecf852000fe35c2ddb7~tplv-k3u1fbpfcp-watermark.image">
<meta property="og:image" content="https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/799e1ef4eecc4c52856b6bf49a5cb539~tplv-k3u1fbpfcp-watermark.image">
<meta property="og:image" content="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/6c06ba33b7d247328e7194fc22e5b407~tplv-k3u1fbpfcp-watermark.image">
<meta property="article:published_time" content="2020-12-20T11:45:23.000Z">
<meta property="article:modified_time" content="2020-12-20T11:46:18.536Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/5bca2c4b390f4823b6a206738c131ca1~tplv-k3u1fbpfcp-watermark.image">
  
    <link rel="alternate" href="/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">

  
<meta name="generator" content="Hexo 5.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://example.com"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-Webpack基础配置-三" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/2020/12/20/Webpack%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE-%E4%B8%89/" class="article-date">
  <time class="dt-published" datetime="2020-12-20T11:45:23.000Z" itemprop="datePublished">2020-12-20</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      Webpack基础配置(三)
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><ul>
<li><p>问：为什么使用loader？  </p>
<ul>
<li>webpack自身只能理解js类型的文件，loader可以将所有类型的文件转换为webpack可以处理的<code>有效模块</code>。 </li>
</ul>
</li>
<li><p>问：本节有哪些loader？</p>
<ul>
<li>style-loader、css-loader、less-loader、sass-loader、postcss-loader</li>
</ul>
</li>
</ul>
<h2 id="css-文件"><a href="#css-文件" class="headerlink" title=".css 文件"></a>.css 文件</h2><p><strong><code>css-loader</code></strong> </p>
<ol>
<li><p>下载相关loader<br> <code>yarn add style-loader css-loader --dev</code></p>
</li>
<li><p>webpack.config.js文件中添加规则</p>
 <figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">   &#123;  </span><br><span class="line">     test: <span class="regexp">/\.css$/</span>,  </span><br><span class="line">     use: [<span class="string">&#x27;style-loader&#x27;</span>, <span class="string">&#x27;css-loader&#x27;</span>]</span><br><span class="line">&#125;		</span><br></pre></td></tr></table></figure>
<p> loader的加载顺序为<code>从右至左</code><br> css-loader：将<code>.css文件的内容转化为js字符串</code><br> style-loader：动态创建<code>&lt;style&gt;</code>标签，将转化的字符放入&lt;style&gt;标签中</p>
</li>
<li><p>创建src/css/base.css文件<br> <img src="https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/5bca2c4b390f4823b6a206738c131ca1~tplv-k3u1fbpfcp-watermark.image"><br> base.css文件内容<br> <img src="https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/7410501f54bd46fb8a2609041fbaa3c8~tplv-k3u1fbpfcp-watermark.image"><br> index.html中添加<br> <img src="https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/c84453253e4e45eea029fa63c79c491b~tplv-k3u1fbpfcp-watermark.image"></p>
</li>
<li><p>在入口文件中，引入.css文件<br> <img src="https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/f8df99b0cdeb4e8f9f963fbcd9007e0c~tplv-k3u1fbpfcp-watermark.image"></p>
</li>
<li><p>执行命令<br> yarn build</p>
</li>
<li><p>页面效果<br> <img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/f540e4471725481e9136521c745db58b~tplv-k3u1fbpfcp-watermark.image"></p>
</li>
</ol>
<h2 id="处理-less文件"><a href="#处理-less文件" class="headerlink" title="处理.less文件"></a>处理.less文件</h2><p><strong><code>less-loader</code></strong></p>
<ol>
<li><p>下载相关loader<br> <code>yarn add style-loader css-loader --dev</code><br> <code>yarn add less-loader less --dev</code>  </p>
<blockquote>
<p><code>注：less为转换.less文件命令</code></p>
</blockquote>
</li>
<li><p>webpack.config.js文件中添加规则</p>
 <figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  test: <span class="regexp">/\.less$/</span>,</span><br><span class="line">  use: [<span class="string">&#x27;style-loader&#x27;</span>, <span class="string">&#x27;css-loader&#x27;</span>, <span class="string">&#x27;less-loader&#x27;</span>]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p> loader的加载顺序为<code>从右至左</code><br> less-loader：将<code>.less文件转换为.css文件</code><br> css-loader：将<code>.css文件的内容转化为js字符串</code><br> style-loader：动态创建<code>&lt;style&gt;</code>标签，将转化的字符放入&lt;style&gt;标签中</p>
</li>
<li><p>创建src/less/base.less文件<br> <img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/76f1b62d490041efadbe81bff047af5e~tplv-k3u1fbpfcp-watermark.image"></p>
</li>
<li><p>在入口文件中，引入.less文件<br> <img src="https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/67f217a66880484cb0c4ef6a93746c50~tplv-k3u1fbpfcp-watermark.image"><br> less文件内容<br> <img src="https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/224dd269b7994ccfa5ae976e55306d16~tplv-k3u1fbpfcp-watermark.image"><br> index.html中添加<br> <img src="https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/f8f19544a4ad4231a8c430cb5ccea6a0~tplv-k3u1fbpfcp-watermark.image"></p>
</li>
<li><p>执行命令<br> yarn build</p>
</li>
<li><p>页面效果<br> <img src="https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/9139fb82e8894cdfa5e790545ba0b74f~tplv-k3u1fbpfcp-watermark.image"></p>
</li>
</ol>
<h2 id="处理-scss-sass文件"><a href="#处理-scss-sass文件" class="headerlink" title="处理.scss/.sass文件"></a>处理.scss/.sass文件</h2><p><strong><code>sass-loader</code></strong></p>
<ol>
<li><p>下载相关loader<br> <code>yarn add style-loader css-loader --dev</code><br> <code>yarn add sass-loader node-sass --dev</code></p>
<blockquote>
<p><code>注：node-sass为转换.scss/.sass文件命令</code><br><code>注：如果node-sass安装失败，则执行 yarn config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/ -g</code></p>
</blockquote>
</li>
<li><p>webpack.config.js文件中添加规则</p>
 <figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  test: <span class="regexp">/\.s(a|c)ss$/</span>,</span><br><span class="line">  use: [<span class="string">&#x27;style-loader&#x27;</span>, <span class="string">&#x27;css-loader&#x27;</span>, <span class="string">&#x27;sass-loader&#x27;</span>]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p> loader的加载顺序为<code>从右至左</code><br> sass-loader：将<code>.scss/.sass文件转换为.css文件</code><br> css-loader：将<code>.css文件的内容转化为js字符串</code><br> style-loader：动态创建<code>&lt;style&gt;</code>标签，将转化的字符放入&lt;style&gt;标签中</p>
</li>
<li><p>创建src/scss/base.scss文件<br> <img src="https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/1e849eaa11ba49499d42961f3d51e568~tplv-k3u1fbpfcp-watermark.image"><br> .scss文件内容<br> <img src="https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/33f41c78c752485ebb2d4844d26c3634~tplv-k3u1fbpfcp-watermark.image"><br> index.html中添加<br> <img src="https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/6f74173328f746759f61f7160e9bc898~tplv-k3u1fbpfcp-watermark.image"></p>
</li>
<li><p>在入口文件中，引入.scss文件<br> <img src="https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/991024b545284d719040e0f55bc38278~tplv-k3u1fbpfcp-watermark.image"><br> .scss文件内容<br> <img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/f6cd7df5e9ad49b98f0974ecb0353972~tplv-k3u1fbpfcp-watermark.image"><br> index.html中添加<br> <img src="https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/bb49c688635445a6be54c2fe51059567~tplv-k3u1fbpfcp-watermark.image"></p>
</li>
<li><p>执行命令<br> yarn build</p>
</li>
<li><p>页面效果<br> <img src="https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/04fad7fb512248f59c46dc71e7507436~tplv-k3u1fbpfcp-watermark.image"></p>
</li>
</ol>
<h2 id="样式适配浏览器，自动添加前缀，增强项目兼容性"><a href="#样式适配浏览器，自动添加前缀，增强项目兼容性" class="headerlink" title="样式适配浏览器，自动添加前缀，增强项目兼容性"></a>样式适配浏览器，自动添加前缀，增强项目兼容性</h2><p><strong><code>postcss-loader</code></strong></p>
<ol>
<li><p>下载loader及相关<br> <code>yarn add postcss-loader autoprefixer --dev</code>  </p>
<blockquote>
<p><code>注：autoprefixer是PostCSS插件，可以自动添加所需的带前缀的属性声明。</code><br><code>注：postcss也需要配置browserslist</code></p>
</blockquote>
</li>
<li><p>postcss.config.js(postcss配置文件)</p>
 <figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">	<span class="built_in">module</span>.exports = &#123;</span><br><span class="line">   plugins: [</span><br><span class="line">       <span class="comment">// 引入插件 </span></span><br><span class="line">       <span class="built_in">require</span>(<span class="string">&#x27;autoprefixer&#x27;</span>)</span><br><span class="line">   ]</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure></li>
<li><p>webpack.config.js文件中修改规则<br> <img src="https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/7a54d55a11594ad0910931e93b5dcf39~tplv-k3u1fbpfcp-watermark.image">  </p>
<blockquote>
<p><code>注：postcss-loader将(.less或.scss)转换后的.css文件添加前缀</code></p>
</blockquote>
</li>
<li><p>修改base.css、base.less、base.scss文件(添加带前缀的属性)<br> <img src="https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/0e640732458645a790453b9bc658f28d~tplv-k3u1fbpfcp-watermark.image"></p>
</li>
<li><p>执行命令<br> yarn build</p>
</li>
<li><p>生成代码效果<br> css：<br> <img src="https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/1e07ad8e71074ecf852000fe35c2ddb7~tplv-k3u1fbpfcp-watermark.image"></p>
<p> less：<br> <img src="https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/799e1ef4eecc4c52856b6bf49a5cb539~tplv-k3u1fbpfcp-watermark.image"></p>
<p> scss：<br> <img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/6c06ba33b7d247328e7194fc22e5b407~tplv-k3u1fbpfcp-watermark.image"></p>
</li>
</ol>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2020/12/20/Webpack%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE-%E4%B8%89/" data-id="ckjsoehch0002fsr009r20r6b" data-title="Webpack基础配置(三)" class="article-share-link">Share</a>
      
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2021/01/11/Webpack%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE-%E5%9B%9B/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          Webpack基础配置(四)
        
      </div>
    </a>
  
  
    <a href="/2020/12/20/Webpack%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE-%E4%BA%8C/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">Webpack基础配置(二)</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/01/">January 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/12/">December 2020</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2021/01/11/Webpack%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE-%E5%9B%9B/">Webpack基础配置(四)</a>
          </li>
        
          <li>
            <a href="/2020/12/20/Webpack%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE-%E4%B8%89/">Webpack基础配置(三)</a>
          </li>
        
          <li>
            <a href="/2020/12/20/Webpack%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE-%E4%BA%8C/">Webpack基础配置(二)</a>
          </li>
        
          <li>
            <a href="/2020/12/20/Webpack%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE-%E4%B8%80/">Webpack基础配置(一)</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2021 John Doe<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/js/jquery-3.4.1.min.js"></script>



  
<script src="/fancybox/jquery.fancybox.min.js"></script>




<script src="/js/script.js"></script>





  </div>
</body>
</html>